	<?php
	ob_end_clean();
	ob_start();
?>
<html xmlns="http://www.w3.org/1999/xhtml" lang="ja" xml:lang="ja">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<meta http-equiv="Content-Script-Type" content="text/javascript" />
		<meta http-equiv="Content-Style-Type" content="text/css" />
	</head>
<body>
<?php
	require_once( '../system/smarty.inc' );
	require_once( '../system/mdao/Class_M_AREA_AD.php' );
	require_once( '../system/mdao/Class_M_AREA_AD_SHOP.php' );
	
	require_once( '../system/mdao/Class_M_OTHER.php' );
	require_once( '../system/Class_FILE_CSV.php' );
	require_once( '../system/Class_ERROR.php' );
	require_once( '../system/login.inc.php' );
	require_once( '../system/aoki_m.php' );
	require_once( '../system/Class_PWD.php' );
	
	Class_PWD::valid_auth(AOKI_AUTH_AREA_AD,$login['staff_auth']);
	
	$isG5 = FALSE ;
	if   ($login['staff_auth'] == 'G-5') {
		$isG5 = TRUE ;
	}
	
	$docsv_import = FALSE;
	$matrix_cd    = $_POST["week_cd"];
	//echo $matrix_cd."******";
	
	$csvf = Class_FILE_CSV::get_upload_file();
	
	if ($csvf) {
		// ロケール情報の変更
		$locale = setlocale(LC_ALL, 0);
		setlocale(LC_ALL,'ja_JP.UTF-8');
		$rowall = Class_FILE_CSV::fget_rows($csvf);
		
		if  (!empty($rowall)) {
				//--------11地域広告inCSV-------開始------------
				$col_heads = array(
				'area_cd'           , //エリアコード
				''                  , //エリア名
				'publish_area_name' , //配布エリア名
				'area_nums'         , //エリア部数
				'hand_name'         , //取扱業者
				'media_name'        , //媒体名
				''                  , //matrix_cd
				'memo'                //備考
				);
				$csvheads  = array();
				$col_index = array();
				foreach ($col_heads as $k => $v) {
					if (!empty($v)) {
						$csvheads[$k]  = $v;
						$col_index[$v] = $k;
					}
				} 
				
				$index_area_cd        = $col_index['area_cd'];      //
				$col_area_cd          = $index_area_cd + 1;
				
				$index_media_name     = $col_index['media_name'];      //
				$col_media_name       = $index_media_name + 1;
				
				$key1 = $col_index['area_cd']; //キーです、必ず仕様します
				//$key2 = $col_index['area_cd']; //キーです、必ず仕様します
				$err = new Class_ERROR("","");
				$dao = new Class_M_AREA_AD;
				
				$ngline  = 0;
				$error_times = 0;
				$error_stop = IMPORT_STOP_MAX_ERROR_TIMES;
				$errtext = array();
				$isng = FALSE;
				$chk = $dao->get_checks();
				
				$area_arr    = Class_M_OTHER::get_area_array();
				$media_arr   = Class_M_OTHER::get_media_array();
				
				$linecount = count($rowall);
				$percentold = 100;
	 			foreach ($rowall as $line => $row) {
					$isng = FALSE;
					$percent = intval($line/$linecount * 100)."%";
					if  ( $percent != $percentold ) {
						Class_FILE_CSV::write_msg_checking_per( $percent ) ;
					}
					$percentold = $percent;
					
					//CSV内部重複不可チェック
					foreach ($rowall as $kk=>$vv) {
						if  ($line != $kk) {
							if ($row[$key1] == $vv[$key1]){
								$col = $key1 + 1;
								$isng = TRUE;
								$ngMsg_tmp[$line][] = "{$line}行と{$kk}行の{$col}列目の値重複不可。";
								$error_times++;
								if ($error_times > $error_stop) {
									break;
								}
							}
						}
					} 
					
					foreach ( $csvheads as $k=>$v ) {
						$msg =  $err->check( $rowall[$line][$k], $chk[$v] ); 
						if ( !empty($chk[$v]) ) {
							if (!empty($msg)) {
								$isng = TRUE;
								$col  = $k+1;
								$ngMsg_tmp[$line][] = "{$line}行{$col}列目：{$msg}。";
								$error_times++;					
								if ($error_times > $error_stop) {
									break;
								}
							}
						}
					}
					
					//媒体
					if ( !empty($row[$index_media_name])){
						if (!(in_array ($row[$index_media_name], $media_arr) )) {
							$isng = TRUE;
							$error_times++;
							$ngMsg_tmp[$line][] = "{$line}行{$col_media_name}列目：媒体は存在しません。</br>";
						}
					} 
					
					//エリア
					if ( !empty($row[$index_area_cd])){
						if (empty($area_arr[$row[$index_area_cd]])) {
							$isng = TRUE;
							$error_times++;
							$ngMsg_tmp[$line][] = "{$line}行{$col_area_cd}列目：エリアは存在しません。</br>";
						}
					} 
					
					$where = sprintf(" matrix_cd =  '{$matrix_cd}' AND area_cd = '%s'", $row[$index_area_cd] );
					
					if ( $dao->exits( $where ) ) {
						$isng = TRUE;
						$error_times++;
						$ngMsg_tmp[$line][] = "{$line}行{$col_area_cd}列目：当週のエリアは既に存在します。</br>";
					}
						
					//for (i)lnews1
					if ( $isng ) {
						++$ngline;
						$errtext[$ngline] = $row;
						if ($error_times > $error_stop) {
							break;
						}
					}
				}
				
				if ($ngline > 0) {
					$errHtml = Class_FILE_CSV::format_msg($ngMsg_tmp);
					if ($error_times > $error_stop) {
						Class_FILE_CSV::write_msg_error( $errHtml ,$error_stop );	
					
					} else {
						Class_FILE_CSV::write_msg_error( $errHtml );	
					}
				} else {
					//DBへ更新
					try {
						$daodb = $dao->get_db();
						$dao->begin_trans();
						$i = 0;
						$percent = "";
						$percentold = "";
						foreach ( $rowall as $k=>$v ) {
							$i++;
							$percent = intval($i/$line * 100)."%";
							if  ( $percent != $percentold ) {
								Class_FILE_CSV::write_msg_doing( $percent ) ;
							}
							$percentold = $percent;
							$values  = array();
							
							foreach ( $csvheads as $kk => $vv ) {
								$values[$vv] = $v[$kk];
							}
							
							$keyvalues =  $values[$col_heads[$key1]];
							$where     = sprintf(" matrix_cd =  {$matrix_cd} AND {$col_heads[$key1]} = '%s'", $keyvalues );
							$values["matrix_cd"]  =   $matrix_cd;
							$values["hand_cd"]           =  Class_M_OTHER::do_hand($values["hand_name"], $daodb);
							$values["publish_area_cd"]   =  Class_M_OTHER::do_publish_area($values["publish_area_name"], $daodb);
							$values["area_name"]         =  $area_arr[$values["area_cd"]];
							$values["media_cd"]          =  array_search($values["media_name"], $media_arr) ;
							//$values["stop_date"]         =  Class_PWD::date_to_int8 ( $values['stop_date'] );
							
							if ( !( $dao->add($values ) ) ) {
								throw Exception("mareaad_import db更新");
							}
						}
						Class_FILE_CSV::write_msg_succed($line, "mareaad");
						$dao->commit_trans();
						
					} catch (Exception $e) {
						$dao->rollback_trans();
						Class_FILE_CSV::write_msg_error( "DB Exceptionがありました。</br>" );
					}
				} 
				//--------11地域広告inCSV-------完了------------
		}else {
			$errHtml = "CSVファイルエラー";
			Class_FILE_CSV::write_msg_error( $errHtml );
		}
		// ロケール情報の復元
		setlocale(LC_ALL, $locale);
	}else {
		$errHtml = "CSVファイルエラー";
		Class_FILE_CSV::write_msg_error( $errHtml );
	}
?>
</body>
	</himl>
	